package com.tsd.core.utils;

import com.tsd.core.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ResourcesUtil
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2022/9/19 11:02
 * @Version: 1.0
 */
public class ResourcesUtil {

    public static final Logger LOGGER = LoggerFactory.getLogger(ResourcesUtil.class);

    /**
     * 获取classpath下xlsx目录的文件路径
     *
     * @param templateName
     * @return
     */
    public static String getXlsxPath(String templateName) {
        List<String> templatesPathList = getClassPath(Constants.CLASS_PATH_XLSX + templateName);
        if (!HlpUtils.isEmptyList(templatesPathList)) {
            return templatesPathList.get(0);
        }
        return null;
    }

    /**
     * 获取classpath下excelTemplate目录的文件路径
     *
     * @param templateName
     * @return
     */
    public static String getExcelTemplatePath(String templateName) {
        List<String> templatesPathList = getClassPath(Constants.CLASS_PATH_EXCEL_TEMPLATE + templateName);
        if (!HlpUtils.isEmptyList(templatesPathList)) {
            return templatesPathList.get(0);
        }
        return null;
    }

    /**
     * 获取classpath下import目录的文件路径
     *
     * @param configName
     * @return
     */
    public static String getExcelImportPath(String configName) {
        List<String> templatesPathList = getClassPath(Constants.CLASS_PATH_IMPORT + configName);
        if (!HlpUtils.isEmptyList(templatesPathList)) {
            return templatesPathList.get(0);
        }
        return null;
    }

    /**
     * 获取classpath下templates目录的文件路径
     *
     * @param name
     * @return
     */
    public static String getTemplatesPath(String name) {
        List<String> templatesPathList = getClassPath(Constants.CLASS_PATH_TEMPLATES + name);
        if (!HlpUtils.isEmptyList(templatesPathList)) {
            return templatesPathList.get(0);
        }
        return null;
    }

    /**
     * 获取classpath的文件路径列表
     *
     * @param classPath
     * @return
     */
    public static List<String> getClassPath(String classPath) {
        List<String> pathList = new ArrayList<>();
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            Resource[] resources = resolver.getResources(classPath);
            for (Resource resource : resources) {
                String path = URLDecoder.decode(resource.getURL().getPath(), "UTF-8");
                pathList.add(path);
            }
        } catch (IOException e) {
            LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
        }
        return pathList;
    }
}
